﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wormhole.Engine.Communication.Messages
{
	/// <author>Hannes Foulds</author>
	/// <date>23 September 2008</date>
	/// <summary>
	/// This is the message to send for a game closed notification.
	/// </summary>
	public class GameClosedMessage : Message
	{
		#region Properties

		#region Game List
		/// <summary>
		/// A list of open games.
		/// </summary>
		public Dictionary<Guid, Game> GameList { get; set; }
		#endregion

		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a new instance of the class.
		/// </summary>
		/// <param name="game">The game the notification is for.</param>
		public GameClosedMessage(Game game)
		{
			this.Action = Actions.GameClosed;
			this.Game = game;
		}

		/// <summary>
		/// Initialize a new instance of the message from its string 
		/// </summary>
		/// <param name="message"></param>
		public GameClosedMessage(string message, Dictionary<Guid, Game> gameList)
		{
			this.Action = Actions.GameClosed;
			this.GameList = gameList;

			this.Parse(message);
		}
		#endregion

		#region To String
		/// <summary>
		/// Convert the message to a string that can be send via the communications channel.
		/// </summary>
		/// <returns>Returns the string representation of the message.</returns>
		public override string ToString()
		{
			return string.Format("{0}|{1}", EnumConvertor<Actions>.ToString(this.Action), this.Game.ID);
		}
		#endregion

		#region Parse
		/// <summary>
		/// Parse a received game list message.
		/// </summary>
		/// <param name="message">The string message to parse.</param>
		private void Parse(string message)
		{
			string[] messageParts = message.Split('|');
			Guid gameID = new Guid(messageParts[1]);

			// get the game the message if for.
			if (this.GameList.ContainsKey(gameID))
			{
				this.Game = this.GameList[gameID];
			}
			else
			{
				this.Game = new Game();
				this.Game.ID = gameID;
			}
		}
		#endregion
	}
}
